package com.wang.test.demo.json2xml;

import cn.hutool.json.JSONObject;
import cn.hutool.json.XML;

public class Json2Xml {
    /**
     * xml字符串转换为json字符串
     * @return
     */
    public static String xml2Json(){
        String xml = "<?xml version=\"1.0\" encoding=\"gbk\"?>" +
                "<business>" +
                "<body >" +
                "<kpzdbs>开票终端标识</kpzdbs>" +
                "<fplxdm>发票类型代码</fplxdm>" +
                "<fpdm>发票代码</fpdm>" +
                "<fphm>发票号码</fphm>" +
                "<dylx>打印类型</dylx>" +
                "<dyfs>打印方式</dyfs>" +
                "<printername>打印机名称</printername>" +
                "</body>" +
                "</business>";
        JSONObject json = XML.toJSONObject(xml);
        return json.toString();
    }

    /**
     * json字符串转换成xml格式字符串
     *
     * @return
     */
    public static String json2Xml() {
        String json = "{\"business\":{\"body\":{\"fpdm\":\"发票代码\",\"fplxdm\":\"发票类型代码\",\"kpzdbs\":\"开票终端标识\",\"dyfs\":\"打印方式\",\"dylx\":\"打印类型\",\"printername\":\"打印机名称\",\"fphm\":\"发票号码\"}}}";
        JSONObject jsonObject = new JSONObject(json);
        String xml = XML.toXml(jsonObject);
        return xml;
    }

    public static void main(String[] args) {
        String s = xml2Json();
        System.out.println(s);
        String s1 = json2Xml();
        System.out.println(s1);
    }
}
